package com.zlx.guguanjia.mapper.provider;

import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.StringUtils;

import java.util.Map;

public class WorkOrderSqlProvider {


    /**
     * 动态查询工单列表
     * @param params  {status:xxx,startDate:xxx,endDate:xxx,oid:xxx}
     * @return
     */
    public String selectPage(Map<String,Object> params){
       return new SQL(){{
           SELECT("wo.*, " +
                   "cu.`name` create_user_name, " +
                   "tu.`name` transfer_user_name, " +
                   "ru.`name` recipient_user_name, " +
                   "co.`name` create_office_name ");
           FROM("work_order wo ");
           LEFT_OUTER_JOIN("sys_user cu ON wo.create_user_id = cu.id " +
                   "LEFT JOIN sys_user tu ON wo.transport_user_id = tu.id " +
                   "LEFT JOIN sys_user ru ON wo.recipient_user_id = ru.id " +
                   "LEFT JOIN sys_office co ON cu.office_id = co.id " +
                   "LEFT JOIN sys_office `to` ON tu.office_id = `to`.id " +
                   "LEFT JOIN sys_office ro ON ru.office_id = ro.id ");
           WHERE("wo.del_flag = '0'");
           if (params.containsKey("status")&&!StringUtils.isEmpty(params.get("status"))){
               WHERE("wo.`status` = #{status}");
           }
           if (params.containsKey("startTime")&&!StringUtils.isEmpty(params.get("startTime"))){
               WHERE("wo.create_date>= #{startTime}");
           }
           if (params.containsKey("endTime")&&!StringUtils.isEmpty(params.get("endTime"))){
               WHERE("wo.create_date <= #{endTime}");
           }
           if (params.containsKey("oid")&&!StringUtils.isEmpty(params.get("oid"))){
               WHERE("(co.id=#{oid} OR `to`.id=#{oid} OR ro.id=#{oid})");
           }
       }}.toString();
    }
}
